// JavaScript Document
var http = require('http')
var fs = require('fs')
var mongoose = require('mongoose');

var webserver  = http.createServer( function (req, res) {
	fs.readFile('fog1.2.6.html', 'utf8', function (err, data) {
		res.writeHead(200, { 'Content-Type': 'text/html' });
		res.end(data);
	});
});


var Schema = mongoose.Schema;

var GridSchema = new Schema({
	_id : String,
	gridx : Number,
	gridy : Number,
	prop : {
		//id:
		//gridx:
		//gridy:
		writime : Number,
		color : String,
		class : String,
		//subject : String,
		keyword : String,
		key_id: String,
		content : String,
		//good : Number,
		//bad : Number,
		opacity : Number
	},
	relaArrays : {
	nearBy : [Boolean],
	relaIn : [Boolean],
	relaOut : [Boolean],	
	nearbyG : []
	}
});

var TpSchema = new Schema({
	grid_id : String,
	timepoint : Number,
	level : Number
});

var db = mongoose.createConnection('localhost', 'fog')
	, GridModel = db.model('fullopengrid', GridSchema)
	, GridData = db.model('fullopengrid')
	, TpData = db.model('tp', TpSchema)
	, TpData = db.model('tp')

//app.get

var io = require('socket.io').listen(webserver);
io.set('log level', 1);

//var color, keyword, content;

var gridid;
var keyid;

io.sockets.on('connection', function (socket) {
	
	socket.on('ajaj', function (data) {
		
		GridData.find({}, {'relaArrays':0})
		.where('gridx').gte(data.cfx-64).lt(data.cfx+64)
		.where('gridy').gte(data.cfy-24).lt(data.cfy+24)
		.where('prop.class').in(['filled', 'relaempty', 'reserved'])
		.exec(function (err, gridb){
			socket.emit('ajaj', gridb);
		});
	});
	socket.on('reserve', function (data) {
		console.log('R')
		console.log(data.id);
		GridData.findById(data.id, function(err, gridb) {
			gridb.prop.class = 'reserved';
			gridb.save();
			console.log(gridb);
			io.sockets.emit('reserve', gridb);
		});
	});
	socket.on('reserveC', function (data) {
		console.log('C')
		console.log(data);
		if (data.id) {
			GridData.findById(data.id, function(err, gridb) {
				if (gridb.prop.class == "reserved" && gridb.relaArrays.nearbyG[0] == null) {
					
					gridb.prop = null;
					gridb.save();
					console.log(gridb);
					io.sockets.emit('reserveC', gridb);
				}
				else if (gridb.prop.class == "reserved" && gridb.relaArrays.nearbyG[0] != null) {
					gridb.prop.class = 'relaempty';
					gridb.save();
					console.log(gridb);
					io.sockets.emit('reserveC', gridb);
				}
			});
		}
	});
	socket.on('regist', function (data) {
		if (data.key_id == null) {
			keyid = Number(new Date()) + '' + (Math.floor(Math.random()*1000));
		}
		else if (data.key_id != null) {
			keyid = data.key_id;
		}
		GridData.findById(data.id, function(err, gridb) {
			gridb.prop.color = data.fill;
			gridb.prop.keyword = data.keyword;
			gridb.prop.key_id = keyid;
			gridb.prop.content = data.message;
			gridb.prop.class = 'filled';
			gridb.prop.writime = Number(new Date());
			gridb.relaArrays.nearBy[0] = 1;
			gridb.markModified('relaArrays');
			gridb.save();
			
			var newTp = new TpData();
			newTp.grid_id = data.id;
			newTp.timepoint = 24000;
			console.log(newTp);
			newTp.save();
			io.sockets.emit('registpush', gridb);
		});		
		GridData.findById(data.id1, function(err, gridb) {
			if (gridb.prop.class == null) {
				gridb.relaArrays.nearBy[4] = true;
				gridb.prop.class = 'relaempty';
				gridb.prop.key_id = keyid;
				gridb.prop.keyword = data.keyword
				gridb.prop.color = data.fill;
				gridb.markModified('relaArrays');
				gridb.relaArrays.nearbyG.push(data.id);
				gridb.save();
				io.sockets.emit('registpushN', gridb);
			}
			else if (gridb.prop.class == 'relaempty' &&  gridb.prop.key_id == keyid) {
				gridb.relaArrays.nearBy[4] = true;
				gridb.markModified('relaArrays');
				gridb.relaArrays.nearbyG.push(data.id);
				gridb.save();
			}
			else {
				gridb.relaArrays.nearBy[4] = true;
				gridb.markModified('relaArrays');
				gridb.relaArrays.nearbyG.push(data.id);
				gridb.save();
			}
		});		
		GridData.findById(data.id2, function(err, gridb) {
			console.log(2);
			if (gridb.prop.class == null) {
				gridb.relaArrays.nearBy[5] = true;
				gridb.prop.class = 'relaempty';
				gridb.prop.key_id = keyid;
				gridb.prop.keyword = data.keyword;
				gridb.prop.color = data.fill;
				gridb.markModified('relaArrays');
				gridb.relaArrays.nearbyG.push(data.id);
				gridb.save();
				io.sockets.emit('registpushN', gridb);
			}
			else if (gridb.prop.class == 'relaempty' &&  gridb.prop.key_id == keyid) {
				gridb.relaArrays.nearBy[5] = true;
				gridb.markModified('relaArrays');
				gridb.relaArrays.nearbyG.push(data.id);
				gridb.save();
			}
			else {
				gridb.relaArrays.nearBy[5] = true;
				gridb.markModified('relaArrays');
				gridb.relaArrays.nearbyG.push(data.id);
				gridb.save();
			}
		});
		GridData.findById(data.id3, function(err, gridb) {
			if (gridb.prop.class == null) {
				gridb.relaArrays.nearBy[6] = true;
				gridb.prop.class = 'relaempty';
				gridb.prop.key_id = keyid;
				gridb.prop.keyword = data.keyword;
				gridb.prop.color = data.fill;
				gridb.markModified('relaArrays');
				gridb.relaArrays.nearbyG.push(data.id);
				gridb.save();
				io.sockets.emit('registpushN', gridb);
			}
			else if (gridb.prop.class == 'relaempty' &&  gridb.prop.key_id == keyid) {
				gridb.relaArrays.nearBy[6] = true;
				gridb.markModified('relaArrays');
				gridb.relaArrays.nearbyG.push(data.id);
				gridb.save();
			}
			else {
				gridb.relaArrays.nearBy[6] = true;
				gridb.markModified('relaArrays');
				gridb.relaArrays.nearbyG.push(data.id);
				gridb.save();
			}
		});
		GridData.findById(data.id4, function(err, gridb) {
			if (gridb.prop.class == null) {
				gridb.relaArrays.nearBy[1] = true;
				gridb.prop.class = 'relaempty';
				gridb.prop.key_id = keyid;
				gridb.prop.keyword = data.keyword;
				gridb.prop.color = data.fill;
				gridb.markModified('relaArrays');
				gridb.relaArrays.nearbyG.push(data.id);
				gridb.save();
				io.sockets.emit('registpushN', gridb);
			}
			else if (gridb.prop.class == 'relaempty' &&  gridb.prop.key_id == keyid) {
				gridb.relaArrays.nearBy[1] = true;
				gridb.markModified('relaArrays');
				gridb.relaArrays.nearbyG.push(data.id);
				gridb.save();
			}
			else {
				gridb.relaArrays.nearBy[1] = true;
				gridb.markModified('relaArrays');
				gridb.relaArrays.nearbyG.push(data.id);
				gridb.save();
			}
		});
		GridData.findById(data.id5, function(err, gridb) {
			if (gridb.prop.class == null) {
				gridb.relaArrays.nearBy[2] = true;
				gridb.prop.class = 'relaempty';
				gridb.prop.key_id = keyid;
				gridb.prop.keyword = data.keyword;
				gridb.prop.color = data.fill;
				gridb.prop.markModified('relaArrays');
				gridb.relaArrays.nearbyG.push(data.id);
				gridb.save();
				io.sockets.emit('registpushN', gridb);
			}
			else if (gridb.prop.class == 'relaempty' &&  gridb.prop.key_id == keyid) {
				gridb.relaArrays.nearBy[2] = true;
				gridb.markModified('relaArrays');
				gridb.relaArrays.nearbyG.push(data.id);
				gridb.save();
			}
			else {
				gridb.relaArrays.nearBy[2] = true;
				gridb.markModified('relaArrays');
				gridb.relaArrays.nearbyG.push(data.id);
				gridb.save();
			}
		});
		GridData.findById(data.id6, function(err, gridb) {
			if (gridb.prop.class == null) {
				gridb.relaArrays.nearBy[3] = true;
				gridb.prop.class = 'relaempty';
				gridb.prop.key_id = keyid;
				gridb.prop.keyword = data.keyword;
				gridb.prop.color = data.fill;
				gridb.markModified('relaArrays');
				gridb.relaArrays.nearbyG.push(data.id);
				gridb.save();
				io.sockets.emit('registpushN', gridb);
			}
			else if (gridb.prop.class == 'relaempty' &&  gridb.prop.key_id == keyid) {
				gridb.relaArrays.nearBy[3] = true;
				gridb.markModified('relaArrays');
				gridb.relaArrays.nearbyG.push(data.id);
				gridb.save();
			}
			else {
				gridb.relaArrays.nearBy[3] = true;
				gridb.markModified('relaArrays');
				gridb.relaArrays.nearbyG.push(data.id);
				gridb.save();
			}
		});		
	});
		setInterval(function() {
			TpData.find({}, function (err, tpdbs) {
				tpdbs.forEach(function (tpdb) {
					var pastLevel = tpdb.level
					/*if (tpdb.timepoint >300000) {
						tpdb.level = 6; 
					}
					else if (tpdb.timepoint > 240000 && tpdb.timepoint <= 300000) {
						tpdb.level = 5;
					}*/
					if (tpdb.timepoint >24000) {
						tpdb.level = 5; 
					}
					else if (tpdb.timepoint > 18000 && tpdb.timepoint <= 24000) {
						tpdb.level = 4;
					}
					else if (tpdb.timepoint > 12000 && tpdb.timepoint <= 18000) {
						tpdb.level = 3;
					}
					else if (tpdb.timepoint > 6000 && tpdb.timepoint <= 12000) {
						tpdb.level = 2;
					}
					else if (tpdb.timepoint > 0 && tpdb.timepoint <= 6000) {
						tpdb.level = 1;
					}
					else { // 지워지는 좌표안으로 먼저 들어간다
						GridData.findById(tpdb.grid_id, function(err, gridb) {
							console.log(gridb);
							gridb.relaArrays.nearBy[0] = null;
							gridb.markModified('relaArrays');
							var id1 = 'g' + (gridb.gridx - 1) + 'x' + (gridb.gridy - 1) + 'y';
							var id2 = 'g' + (gridb.gridx - 2) + 'x' + (gridb.gridy) + 'y';
							var id3 = 'g' + (gridb.gridx - 1) + 'x' + (gridb.gridy + 1) + 'y';
							var id4 = 'g' + (gridb.gridx + 1) + 'x' + (gridb.gridy + 1) + 'y';
							var id5 = 'g' + (gridb.gridx + 2) + 'x' + (gridb.gridy) + 'y';
							var id6 = 'g' + (gridb.gridx + 1) + 'x' + (gridb.gridy - 1) + 'y';
							if (gridb.relaArrays.nearBy[1] != null || gridb.relaArrays.nearBy[2] != null || gridb.relaArrays.nearBy[3] != null || gridb.relaArrays.nearBy[4] != null || gridb.relaArrays.nearBy[5] != null || gridb.relaArrays.nearBy[6] != null) {
								var id = gridb.relaArrays.nearbyG[0];
								GridData.findById(id, function(err, gridb2) {
									gridb.prop.class = "relaempty";
									gridb.prop.content = null;
									gridb.prop.writime = null;
									gridb.prop.color = gridb2.prop.color;
									gridb.prop.keyword = gridb2.prop.keyword;
									gridb.prop.key_id = gridb2.prop.key_id;
									io.sockets.emit('removepushN', gridb);
									gridb.save();
								});
							}
							else {
								gridb.prop = null;
								io.sockets.emit('removepush', gridb._id);
							}
							gridb.save();
							
							GridData.findById(id1, function(err, gridbIn) {
								gridbIn.relaArrays.nearBy[4] = null;
								var NGInd = gridbIn.relaArrays.nearbyG.indexOf(gridb.id);
								gridbIn.relaArrays.nearbyG.splice(NGInd, 1);
								gridbIn.markModified('relaArrays');
								if (gridbIn.relaArrays.nearBy[0] == null && gridbIn.relaArrays.nearBy[1] == null && gridbIn.relaArrays.nearBy[2] == null && gridbIn.relaArrays.nearBy[3] == null && gridbIn.relaArrays.nearBy[4] == null && gridbIn.relaArrays.nearBy[5] == null && gridbIn.relaArrays.nearBy[6] == null) {
									gridbIn.prop = null;
									io.sockets.emit('removepush', gridbIn._id);
								}
								else if (gridbIn.relaArrays.nearBy[0] == null) {
									var id = gridbIn.relaArrays.nearbyG[0]
									GridData.findById(id, function(err, gridbInIn) {
										gridbIn.prop.class = "relaempty";
										gridbIn.prop.content = null;
										gridbIn.prop.writime = null;
										gridbIn.prop.color = gridbInIn.prop.color;
										gridbIn.prop.keyword = gridbInIn.prop.keyword;
										gridbIn.prop.key_id = gridbInIn.prop.key_id;
										io.sockets.emit('removepushN', gridbIn);
										//gridb.save();
									});
								}
								gridbIn.save();			
							});
							
							GridData.findById(id2, function(err, gridbIn) {
								gridbIn.relaArrays.nearBy[5] = null;
								var NGInd = gridbIn.relaArrays.nearbyG.indexOf(gridb.id);
								gridbIn.relaArrays.nearbyG.splice(NGInd, 1);
								gridbIn.markModified('relaArrays');
								if (gridbIn.relaArrays.nearBy[0] == null && gridbIn.relaArrays.nearBy[1] == null && gridbIn.relaArrays.nearBy[2] == null && gridbIn.relaArrays.nearBy[3] == null && gridbIn.relaArrays.nearBy[4] == null && gridbIn.relaArrays.nearBy[5] == null && gridbIn.relaArrays.nearBy[6] == null) {
									gridbIn.prop = null;
									io.sockets.emit('removepush', gridbIn._id);
								}
								else if (gridbIn.relaArrays.nearBy[0] == null) {
									var id = gridbIn.relaArrays.nearbyG[0]
									GridData.findById(id, function(err, gridbInIn) {
										gridbIn.prop.class = "relaempty";
										gridbIn.prop.content = null;
										gridbIn.prop.writime = null;
										gridbIn.prop.color = gridbInIn.prop.color;
										gridbIn.prop.keyword = gridbInIn.prop.keyword;
										gridbIn.prop.key_id = gridbInIn.prop.key_id;
										io.sockets.emit('removepushN', gridbIn);
										//gridb.save();
									});
								}
								gridbIn.save();			
							});
							
							GridData.findById(id3, function(err, gridbIn) {
								gridbIn.relaArrays.nearBy[6] = null;
								var NGInd = gridbIn.relaArrays.nearbyG.indexOf(gridb.id);
								gridbIn.relaArrays.nearbyG.splice(NGInd, 1);
								gridbIn.markModified('relaArrays');
								if (gridbIn.relaArrays.nearBy[0] == null && gridbIn.relaArrays.nearBy[1] == null && gridbIn.relaArrays.nearBy[2] == null && gridbIn.relaArrays.nearBy[3] == null && gridbIn.relaArrays.nearBy[4] == null && gridbIn.relaArrays.nearBy[5] == null && gridbIn.relaArrays.nearBy[6] == null) {
									gridbIn.prop = null;
									io.sockets.emit('removepush', gridbIn._id);
								}
								else if (gridbIn.relaArrays.nearBy[0] == null) {
									var id = gridbIn.relaArrays.nearbyG[0]
									GridData.findById(id, function(err, gridbInIn) {
										gridbIn.prop.class = "relaempty";
										gridbIn.prop.content = null;
										gridbIn.prop.writime = null;
										gridbIn.prop.color = gridbInIn.prop.color;
										gridbIn.prop.keyword = gridbInIn.prop.keyword;
										gridbIn.prop.key_id = gridbInIn.prop.key_id;
										io.sockets.emit('removepushN', gridbIn);
										//gridb.save();
									});
								}
								gridbIn.save();			
							});
							
							GridData.findById(id4, function(err, gridbIn) {
								gridbIn.relaArrays.nearBy[1] = null;
								var NGInd = gridbIn.relaArrays.nearbyG.indexOf(gridb.id);
								gridbIn.relaArrays.nearbyG.splice(NGInd, 1);
								gridbIn.markModified('relaArrays');
								if (gridbIn.relaArrays.nearBy[0] == null && gridbIn.relaArrays.nearBy[1] == null && gridbIn.relaArrays.nearBy[2] == null && gridbIn.relaArrays.nearBy[3] == null && gridbIn.relaArrays.nearBy[4] == null && gridbIn.relaArrays.nearBy[5] == null && gridbIn.relaArrays.nearBy[6] == null) {
									gridbIn.prop = null;
									io.sockets.emit('removepush', gridbIn._id);
								}
								else if (gridbIn.relaArrays.nearBy[0] == null) {
									var id = gridbIn.relaArrays.nearbyG[0]
									GridData.findById(id, function(err, gridbInIn) {
										gridbIn.prop.class = "relaempty";
										gridbIn.prop.content = null;
										gridbIn.prop.writime = null;
										gridbIn.prop.color = gridbInIn.prop.color;
										gridbIn.prop.keyword = gridbInIn.prop.keyword;
										gridbIn.prop.key_id = gridbInIn.prop.key_id;
										io.sockets.emit('removepushN', gridbIn);
										//gridb.save();
									});
								}
								gridbIn.save();			
							});
							
							GridData.findById(id5, function(err, gridbIn) {
								gridbIn.relaArrays.nearBy[2] = null;
								var NGInd = gridbIn.relaArrays.nearbyG.indexOf(gridb.id);
								gridbIn.relaArrays.nearbyG.splice(NGInd, 1);
								gridbIn.markModified('relaArrays');
								if (gridbIn.relaArrays.nearBy[0] == null && gridbIn.relaArrays.nearBy[1] == null && gridbIn.relaArrays.nearBy[2] == null && gridbIn.relaArrays.nearBy[3] == null && gridbIn.relaArrays.nearBy[4] == null && gridbIn.relaArrays.nearBy[5] == null && gridbIn.relaArrays.nearBy[6] == null) {
									gridbIn.prop = null;
									io.sockets.emit('removepush', gridbIn._id);
								}
								else if (gridbIn.relaArrays.nearBy[0] == null) {
									var id = gridbIn.relaArrays.nearbyG[0]
									GridData.findById(id, function(err, gridbInIn) {
										gridbIn.prop.class = "relaempty";
										gridbIn.prop.content = null;
										gridbIn.prop.writime = null;
										gridbIn.prop.color = gridbInIn.prop.color;
										gridbIn.prop.keyword = gridbInIn.prop.keyword;
										gridbIn.prop.key_id = gridbInIn.prop.key_id;
										io.sockets.emit('removepushN', gridbIn);
										//gridb.save();
									});
								}
								gridbIn.save();			
							});
							
							GridData.findById(id6, function(err, gridbIn) {
								gridbIn.relaArrays.nearBy[3] = null;
								var NGInd = gridbIn.relaArrays.nearbyG.indexOf(gridb.id);
								gridbIn.relaArrays.nearbyG.splice(NGInd, 1);
								gridbIn.markModified('relaArrays');
								if (gridbIn.relaArrays.nearBy[0] == null && gridbIn.relaArrays.nearBy[1] == null && gridbIn.relaArrays.nearBy[2] == null && gridbIn.relaArrays.nearBy[3] == null && gridbIn.relaArrays.nearBy[4] == null && gridbIn.relaArrays.nearBy[5] == null && gridbIn.relaArrays.nearBy[6] == null) {
									/*if (gridbIn.prop.class == 'reserved'){
										gridbIn.prop = null;
										gridbIn.prop.class = 'reserved' 
									}*/
									gridbIn.prop = null;
									io.sockets.emit('removepush', gridbIn._id);
								}
								else if (gridbIn.relaArrays.nearBy[0] == null) {
									var id = gridbIn.relaArrays.nearbyG[0]
									GridData.findById(id, function(err, gridbInIn) {
										gridbIn.prop.class = "relaempty";
										gridbIn.prop.content = null;
										gridbIn.prop.writime = null;
										gridbIn.prop.color = gridbInIn.prop.color;
										gridbIn.prop.keyword = gridbInIn.prop.keyword;
										gridbIn.prop.key_id = gridbInIn.prop.key_id;
										io.sockets.emit('removepushN', gridbIn);
										//gridb.save();
									});
								}
								gridbIn.save();			
							});
							
						});
						tpdb.remove();
					}
					if (pastLevel != tpdb.level || pastLevel == null ) {
						io.sockets.emit('timepoint', tpdb)
					}
					tpdb.timepoint -= 1000;
					tpdb.save();
				});
			});
		}, 1000);
	
});

webserver.listen(7373);